# 使用nvidia/cuda的开发镜像作为基础镜像
FROM nvidia/cuda:11.7.1-devel-ubuntu20.04

# 设置环境变量
ENV DEBIAN_FRONTEND=noninteractive

# 添加缺失的公钥
RUN apt-key del 7fa2af80
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/3bf863cc.pub
RUN apt-key adv --fetch-keys https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu2004/x86_64/7fa2af80.pub

# 安装基本依赖
RUN apt-get update && apt-get install -y \
    python3.8 \
    python3.8-dev \
    python3-pip \
    build-essential \
    cmake \
    git \
    && rm -rf /var/lib/apt/lists/*

# 创建一个符号链接，使python指向python3.8
RUN ln -s /usr/bin/python3.8 /usr/bin/python

# 更新pip
RUN python -m pip install --upgrade pip -i https://pypi.tuna.tsinghua.edu.cn/simple

# 拷贝文件
COPY data_annotation_requirements.txt torch-1.13.1+cu117-cp38-cp38-linux_x86_64.whl torchvision-0.14.1+cu117-cp38-cp38-linux_x86_64.whl /workspace/

# 安装依赖
RUN cd /workspace && pip install torch-1.13.1+cu117-cp38-cp38-linux_x86_64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple \
    && pip install torchvision-0.14.1+cu117-cp38-cp38-linux_x86_64.whl -i https://pypi.tuna.tsinghua.edu.cn/simple \
    && pip install -r data_annotation_requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

# 清理APT缓存，删除文件
RUN apt-get clean && rm -rf /var/lib/apt/lists/* && cd /workspace && rm -f torch-1.13.1+cu117-cp38-cp38-linux_x86_64.whl torchvision-0.14.1+cu117-cp38-cp38-linux_x86_64.whl requirements.txt

# 拷贝文件
# COPY img_detection_groundingdino/ /workspace/img_detection_groundingdino
COPY img_detection_yolo/ /workspace/img_detection_yolo
COPY pcd_detection/ /workspace/pcd_detection

# 编译依赖项
# 设置环境变量 CUDA_HOME
ENV CUDA_HOME=/usr/local/cuda-11.7
ENV LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/cuda-11.7/lib64
ENV TORCH_CUDA_ARCH_LIST="7.0 7.5 8.0 8.6+PTX"
# RUN cd /workspace/img_detection_groundingdino && python setup.py clean && python setup.py develop
RUN cd /workspace/pcd_detection && python setup.py clean && python setup.py develop

# 安装依赖
RUN apt-get update && apt-get install -y libglib2.0-dev libsm6 libxrender1 vim libgl1-mesa-glx xvfb && apt-get clean && rm -rf /var/lib/apt/lists/*

# 拷贝开发的文件
COPY data_annotation_service.py test_data_annotation_service.py data_annotation_background_task.py fuse_detection.py utils.py view_anno.py test_upload.json config.py predictor.py /workspace/
RUN cp /workspace/predictor.py /usr/local/lib/python3.8/dist-packages/ultralytics/engine/ && rm /workspace/predictor.py
# 设置工作目录
WORKDIR /workspace
RUN cd /workspace

# 设置默认的启动命令
# CMD ["xvfb-run -a python service.py --restart=True"]
CMD ["python", "data_annotation_service.py", "--restart=True"]
